package com.apes.scm.account.res.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：币种
 *
 * @author xul
 * @create 2018-02-03 17:55
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Currency.CONST_TABLE_NAME)
public class Currency extends Model {
    public static final String CONST_TABLE_NAME = "res_currency";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "币种")
    @Comment("币种编码")
    private long id;

    @Column(name = "name", nullable = false, length = 60)
    @Comment("币种名称")
    private String name; //币种

    @Column(name = "symbol", nullable = false, length = 6)
    @Comment("货币符号")
    private String symbol; //货币符号

    @Column(name = "rounding", nullable = false, columnDefinition = ApesConst.FIELD_ROUND_16_8)
    @Comment("舍入系数")
    private double rounding; //舍入系数

    @Column(name = "position", nullable = false, length = 6)
    @Comment("符号位置")
    private String position; //符号位置：（"before"：金额前面），（"after"：金额后面）

    @OneToMany(mappedBy = "currency", cascade = CascadeType.ALL,orphanRemoval = true)
    private Set<CurrencyRate> rateIds = new HashSet(); //汇率


    public Set<CurrencyRate> getRateIds() {
        if(rateIds == null) return new HashSet<>();
        for (CurrencyRate currencyRate : rateIds) {
            currencyRate.setCurrency(this);
        }
        return rateIds;
    }

    @Column(name = "valid", nullable = false)
    @Comment("有效标记")
    private boolean valid = true; //有效

    /**
     * 小数点位数
     */
    public int queryDecimalPlaces() {
        return Arith.computeDecimalPlaces(rounding);
    }

    public double round(double amt) {
        return Arith.round(amt, queryDecimalPlaces());
    }

    /**
     * 功能：对比值大小
     */
    public int compare(double amt1, double amt2) {
        return Arith.compare(amt1, amt2, queryDecimalPlaces());
    }

    public boolean isZero(double amt) {
        return Arith.isZero(amt, queryDecimalPlaces());
    }

    /*
     * 功能：货币转换
     */
    public static double compute(Currency fromCurrency, Currency toCurrency, double fromAmt, boolean round) {
        double amt;
        if (toCurrency.getId() == fromCurrency.getId()) {
            amt = fromAmt;
        } else {
            double rate = getConversionRate(fromCurrency, toCurrency);
            amt = fromAmt * rate;
        }
        if (round) amt = toCurrency.round(amt);
        return amt;
    }

    /*
     * 功能：货币转换
     */
    public double compute(double fromAmt, Currency toCurrency, boolean round) {
        return compute(this, toCurrency, fromAmt, round);
    }

    /*
     * 功能：货币之间的汇率换算
     */
    public static double getConversionRate(Currency fromCurrency, Currency toCurrency) {
        return toCurrency.computeCurrentRate() / fromCurrency.computeCurrentRate();
    }

    /*
     * 功能：查询当前货币汇率（暂未实现）
     */
    public double computeCurrentRate(){
        return 1;
    }


}
